package com.kzj.mls.activity;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.kzj.mls.R;
import com.kzj.mls.beans.ServiceBookBean;
import com.kzj.mls.recyclerCard.basic.MaterialListView;
import com.kzj.mls.recyclerCard.basic.OnButtonPressListener;
import com.kzj.mls.recyclerCard.card.Card;
import com.kzj.mls.recyclerCard.card.other.SelectAddressCard;
import com.umeng.analytics.MobclickAgent;

import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by xyg on 2015/11/3.
 */
public class SelectAddressActivity extends GBaseActivity {
    private MaterialListView material_listview;
    private List<ServiceBookBean.HomeAddressEntity> addressBeans;
    private ServiceBookBean serviceBookBean;
    private boolean isRefresh = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentViewFor720_1280(R.layout.template_materlistview);
        findViewByIds();
        initDatas();
    }

    @Override
    protected void findViewByIds() {
        material_listview = (MaterialListView) findViewById(R.id.material_listview);
    }

    @Override
    protected void initDatas() {
        setTopTitle("选择上门服务地址");
        setRightText(View.VISIBLE, "管理");
        setLeftImg(View.VISIBLE, R.drawable.back);
        EventBus.getDefault().register(this);
        serviceBookBean = (ServiceBookBean) getIntent().getSerializableExtra("beans");
        addressBeans = serviceBookBean.getAddr();
        fillArrayInListView(addressBeans);
    }

    @Override
    protected void fillArrayInListView() {
        material_listview.clear();
        for (int i = 0; i < addressBeans.size(); i++) {
            isRefresh = false;
            SelectAddressCard selectAddressCard = new SelectAddressCard(this);
            selectAddressCard.setAddressBean(addressBeans.get(i));
            selectAddressCard.setOnNormalButtonPressedListener(new OnButtonPressListener() {
                @Override
                public void onButtonPressedListener(View view, Card card) {
                    for (int i = 0; i < serviceBookBean.getAddr().size(); i++) {
                        if (serviceBookBean.getAddr().get(i).getIs_default().equals("1")) {
                            serviceBookBean.getAddr().get(i).setIs_default("0");
                            break;
                        }
                    }
                    ((SelectAddressCard) card).getAddressBean().setIs_default("1");
                    EventBus.getDefault().post(serviceBookBean);
                    SelectAddressActivity.this.finish();
                }
            });
            material_listview.add(selectAddressCard);
        }
    }

    public void onEventMainThread(ServiceBookBean.HomeAddressEntity homeAddressEntity) {

    }

    public void onEventMainThread(ServiceBookBean serviceBookBean) {
        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        String name = manager.getRunningTasks(1).get(0).topActivity.getClassName();
        this.serviceBookBean = serviceBookBean;
        addressBeans = serviceBookBean.getAddr();
        if (!name.equals(SelectAddressActivity.class.getName())) {
            fillArrayInListView(addressBeans);
        }
    }

    protected void fillArrayInListView(List<ServiceBookBean.HomeAddressEntity> addressBeans) {
        material_listview.clear();
        for (int i = 0; i < addressBeans.size(); i++) {
            SelectAddressCard selectAddressCard = new SelectAddressCard(this);
            selectAddressCard.setAddressBean(addressBeans.get(i));
            selectAddressCard.setOnNormalButtonPressedListener(new OnButtonPressListener() {
                @Override
                public void onButtonPressedListener(View view, Card card) {
                    for (int i = 0; i < serviceBookBean.getAddr().size(); i++) {
                        if (serviceBookBean.getAddr().get(i).getIs_default().equals("1")) {
                            serviceBookBean.getAddr().get(i).setIs_default("0");
                            break;
                        }
                    }
                    ((SelectAddressCard) card).getAddressBean().setIs_default("1");
                    EventBus.getDefault().post(serviceBookBean);
                    SelectAddressActivity.this.finish();
                }
            });
            material_listview.add(selectAddressCard);
        }
    }

//    private void TaskGetAddressList(String user_checked) {
//        RequestServerManager.getInstance().handleMethod(null, null, null, false,
//                Constant.getRootUrl() + Constant.Action.Action_Address_List, RequestServerManager.getInstance().
//                        getRequestParams(Constant.Action.Action_Address_List, user_checked), BaseResponse.class, new ICallBackForRequestServer<BaseResponse>() {
//                    @Override
//                    public void onSuccess(BaseResponse result) {
//                        addressBeans = JSON.parseArray(result.getRetval().toString(), ServiceBookBean.HomeAddressEntity.class);
//                        serviceBookBean.setAddr(addressBeans);
//                        fillArrayInListView(addressBeans);
//                    }
//
//                    @Override
//                    public void onFail(int type, String errorMsg, String json) {
//                        App.getInstance().showToast(errorMsg + "");
//                    }
//                });
//    }

    @Override
    public void doClickRightText() {
        super.doClickRightText();
        Intent intent = new Intent(this, AddressManagerActivity.class);
        intent.putExtra("beans", serviceBookBean);
        startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(getClass().getSimpleName()); //统计页面(仅有Activity的应用中SDK自动调用，不需要单独写)
        MobclickAgent.onResume(this);          //统计时长
    }
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(getClass().getSimpleName()); // （仅有Activity的应用中SDK自动调用，不需要单独写）保证 onPageEnd 在onPause 之前调用,因为 onPause 中会保存信息
        MobclickAgent.onPause(this);
    }
}
